<!DOCTYPE html>
<html>
  
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  <meta name="author" content="Amos Zhu">
  
  
  <title>SpringBoot整合RabbitMQ——RabbitMQ进阶 | Amos的技术博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="rabbitMQ,Spring Boot,rabbitMQ,">
  

  
  <meta name="description" content="rabbitMQ如何保证如果消息发送失败，保证其消息不丢失、怎么设置消息过期时间以及死信队列是如何在消息消费失败时保证消息不丢失的、如何使用过期时间来实现延迟队列以及rabbitMQ的持久化、消息确认的机制是怎样的？本博文将具体介绍上述内容">

  

  
    <script src="//cdn.jsdelivr.net/npm/leancloud-storage@3.11.1/dist/av-min.js" async></script>
  

  
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
  

  
    <script src="//unpkg.com/valine/dist/Valine.min.js" async></script>
  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"eObxCkTJ6j4GzRxGLgiHyfJa-gzGzoHsz","appkey":"fzRMAFC6MCE11mp7mnvh4Kwe","comment":true,"count":true},
    welcome: {"enable":true,"interval":30},
    start_time: "2019-05-28",
    passwords: ["efe07af7441da2b69c4a41e42e73be4db47f66010a56900788a458354a7373ec", ],
    is_post: true,
    lock: false,
    author: "Amos Zhu",
    share: {"qq":true,"wechat":true},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  <script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>

  
    <link rel="icon" href="/blog/images/favicon.ico">
    <link rel="apple-touch-icon" href="/blog/images/touch-icon.png">
  

  <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">

  
</head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">Amos Zhu</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 当浮躁的心静不下，不妨写些东西或者看会书</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">首页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/redis/" target="_self">redis</a>
      
        <a href="/blog/rabbitMQ/" target="_self">rabbitMQ</a>
      
        <a href="/blog/elasticsearch" target="_self">elasticsearch</a>
      
        <a href="/blog/stuff" target="_self">stuff</a>
      
        <a href="/blog/kafka" target="_self">kafka</a>
      
        <a href="/blog/friends/" target="_self">友链</a>
      
        <a href="/blog/about/" target="_self">关于</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://gitee.com/amos_zhu" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            首页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/redis/" target="_self">
            redis
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/rabbitMQ/" target="_self">
            rabbitMQ
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/elasticsearch" target="_self">
            elasticsearch
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/stuff" target="_self">
            stuff
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/kafka" target="_self">
            kafka
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/friends/" target="_self">
            友链
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/about/" target="_self">
            关于
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2019-07-06
    </span>
    
      <span>
        | <a href="/blog/categories/rabbitMQ/"><i class="fa fa-bookmark"></i>rabbitMQ</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
  </div>
  <h1 class="passage-title">
    SpringBoot整合RabbitMQ——RabbitMQ进阶
  </h1>
  
  <article class="passage-article">
    <br>本博文中的代码实现实在<a href>SpringBoot整合RabbitMQ——消息的发送和接收</a>的基础上实现了，完整的代码可以查看Gitee上的项目<a href="https://gitee.com/amos_zhu/rabbitmq" target="_blank" rel="noopener">rabbitmq</a><p></p>
<h2 id="rabbitMQ如何保证消息的不丢失"><a href="#rabbitMQ如何保证消息的不丢失" class="headerlink" title="rabbitMQ如何保证消息的不丢失"></a>rabbitMQ如何保证消息的不丢失</h2><p>消息的丢失有以下四种情况：</p>
<ol>
<li>消息发送到RabbitMQ服务器，交换机根据自身的类型和路由键无法匹配到队列，导致消息丢失</li>
<li>消息设置了过期时间，消息过期了导致消息丢失</li>
<li>消息不能被正确的消费，导致消息的丢失</li>
<li>因为服务器的崩溃导致消息的丢失</li>
</ol>
<p>针对以上的情况，rabbitMQ提供了不同的解决方案 </p>
<h3 id="消息设置mandatory参数和使用备份交换机"><a href="#消息设置mandatory参数和使用备份交换机" class="headerlink" title="消息设置mandatory参数和使用备份交换机"></a>消息设置mandatory参数和使用备份交换机</h3><h4 id="mandatory参数"><a href="#mandatory参数" class="headerlink" title="mandatory参数"></a>mandatory参数</h4><p>在上篇博文中我们其实已经在配置文件中配置了<code>mandatory</code>的参数，并且在<code>connectionFactory</code>中设置了其中的参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  rabbitmq:</span><br><span class="line">    template:</span><br><span class="line">      mandatory: <span class="literal">true</span></span><br><span class="line">    publisher-confirms: <span class="literal">true</span></span><br><span class="line">    publisher-returns: <span class="literal">true</span></span><br></pre></td></tr></table></figure>
<p>然后再发送消息的时候设置回调函数<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 确认后回调方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> correlationData</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ack</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> cause</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">confirm</span><span class="params">(CorrelationData correlationData, <span class="keyword">boolean</span> ack, String cause)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.logger.info(<span class="string">"confirm-----correlationData:"</span> + correlationData.toString() + <span class="string">"---ack:"</span> + ack + <span class="string">"----cause:"</span> + cause);</span><br><span class="line">    <span class="comment">// TODO 记录日志（数据库或者es）</span></span><br><span class="line">    <span class="keyword">this</span>.handleConfirmCallback(correlationData.getId(), ack, cause);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 失败后回调方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> message</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> replyCode</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> replyText</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> exchange</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> routingKey</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">returnedMessage</span><span class="params">(Message message, <span class="keyword">int</span> replyCode, String replyText, String exchange, String routingKey)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.logger.info(<span class="string">"return-----message:"</span> + message.toString() + <span class="string">"---replyCode:"</span> + replyCode + <span class="string">"----replyText:"</span> + replyText + <span class="string">"----exchange:"</span> + exchange + <span class="string">"----routingKey:"</span> + routingKey);</span><br><span class="line">    <span class="comment">// TODO 记录日志（数据库或者es）</span></span><br><span class="line">    <span class="keyword">this</span>.handleReturnCallback(message, replyCode, replyText, routingKey);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>用户可以在这里对消息进行本地持久化，其实上面的也叫<strong>消息确认模式</strong>，发送端将消息发送给RabbitMQ，rabbitMQ会异步回调Confirm方法，告诉发送方，RabbitMQ服务端有没有收到消息，如果没有收到消息的话，原因是什么。同时会异步回调设置的returnedMessage将发送的消息返回</p>
<h3 id="备份交换机"><a href="#备份交换机" class="headerlink" title="备份交换机"></a>备份交换机</h3><p>除了上面的消息确认模式，还有一种备份交换机的方案也是可以解决消息的丢失问题，具体的逻辑如下：</p>
<ol>
<li>声明一个交换机A，其类型为fanout类型</li>
<li>声明一个交换机B，设置其<code>alternate-exchange</code>属性为交换机A</li>
<li>声明一个队列a，并且与交换机A绑定</li>
<li>声明一个队列b，并且与交换机B绑定，路由键为rb</li>
</ol>
<p>这样我们就实现了备份交换机功能，其业务实现逻辑如下：<br>我们发送一个消息到交换机B上，当路由键等于rb时，消息会正确发送到队列b上，当路由键不等于rb时，即消息不能正确的发送到队列b上，此时就会发送给交换机A，由于交换机A是fanout类型的，所以消息会被进一步发送到队列a上。这样我们就实现了发送方消息的不丢失。</p>
<p><img src="https://s2.ax1x.com/2019/07/03/ZtraQg.png" alt></p>
<p>代码实现这里给出<code>alternaate-exchange</code>的实现<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;String,Object&gt;();</span><br><span class="line">map.put(<span class="string">"alternate-exchange"</span>,<span class="string">"A"</span>);</span><br><span class="line">MqExchange exchange = <span class="keyword">new</span> MqExchange().arguments(map).name(<span class="string">"B"</span>).type(ExchangeTypeEnum.DIRECT.getCode());</span><br><span class="line">amExchangeDeclare.declareExchange(exchange);</span><br></pre></td></tr></table></figure></p>
<h3 id="过期时间和死信队列"><a href="#过期时间和死信队列" class="headerlink" title="过期时间和死信队列"></a>过期时间和死信队列</h3><h4 id="过期时间"><a href="#过期时间" class="headerlink" title="过期时间"></a>过期时间</h4><p>我们常见的购物车订单，一般有这样的需求，在规定的时间内没有付款的话，该订单就会失效，这里常见就是使用消息的过期时间来控制的，在rabbitMQ中实现过期时间有两种方式</p>
<ul>
<li>设置队列的过期时间，则该队列中所有的消息的过期时间都是一样的</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置队列的过期时间</span></span><br><span class="line">Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;String,Object&gt;();</span><br><span class="line">map.put(<span class="string">"x-message-ttl"</span>,<span class="number">6000</span>);</span><br><span class="line">MqQueue queue = <span class="keyword">new</span> MqQueue().name(queueName).arguments(map);</span><br><span class="line">amQueueDeclare.declareQueue(queue);</span><br></pre></td></tr></table></figure>
<p>如果不设置ttl（Time To Live）,则这个消息不会过期，如果将TTL设置为0，则表示除非此时可以直接将消息投递到消费者，否则消息会立即丢弃</p>
<ul>
<li>设置消息的过期时间，只有这个消息存在过期时间，设置消息的过期时间如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置消息的过期时间</span></span><br><span class="line">MessagePostProcessor messagePostProcessor = <span class="keyword">new</span> MessagePostProcessor() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Message <span class="title">postProcessMessage</span><span class="params">(Message message)</span> <span class="keyword">throws</span> AmqpException </span>&#123;</span><br><span class="line">        message.getMessageProperties().setExpiration(<span class="number">6000</span>);</span><br><span class="line">        <span class="keyword">return</span> message;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">sendService.send(exchangeName,routingKey,data, messagePostProcessor, messageId);</span><br></pre></td></tr></table></figure>
<p><strong>注意:使用第一种方式来设置过期时间，一旦消息过期，就会从队列中消除，而采用第二种方式，即使消息过期，也不会马上从队列中消除，因为每条消息是否过期是在即将投递到消费者之期间进行判断的</strong></p>
<p>队列也是有过期时间的，通过<code>x-expires</code>属性来设置的<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;String,Object&gt;();</span><br><span class="line">map.put(<span class="string">"x-expires"</span>,<span class="number">6000</span>);</span><br><span class="line">MqQueue queue = <span class="keyword">new</span> MqQueue().name(queueName).arguments(map);</span><br><span class="line">amQueueDeclare.declareQueue(queue);</span><br></pre></td></tr></table></figure></p>
<h4 id="死信队列"><a href="#死信队列" class="headerlink" title="死信队列"></a>死信队列</h4><p>DLX:Dead-Letter-Exchange:当一个消息在一个队列中变成死信之后，他能被重新发送到另一个交换机中，这个交换机就是DLX。</p>
<p>那么消息满足什么条件就会成为死信呢？</p>
<ul>
<li>消息被拒绝，并且设置requeue参数为false</li>
<li>消息过期</li>
<li>队列达到最大长度</li>
</ul>
<p>那么如何使用死信队列？<br>死信队列一般都是作为其他队列的一个属性来用的，当这个队列中存在死信时，RabbitMQ就会自动将这个消息重新发布到设置的DLX上，进而被路由到另一个队列中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个交换机</span></span><br><span class="line">MqExchange dlxExchange = <span class="keyword">new</span> MqExchange().name(<span class="string">"B"</span>).type(ExchangeTypeEnum.DIRECT.getCode());</span><br><span class="line">amExchangeDeclare.declareExchange(exchange);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个队列时，设置他的属性 x-dead-letter-exchange 为上面定义的交换机</span></span><br><span class="line"> Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;String,Object&gt;();</span><br><span class="line">map.put(<span class="string">"x-dead-letter-exchange "</span>,<span class="string">"dlxExchange"</span>);</span><br><span class="line"><span class="comment">// 也可以为DLX指定路由键 这个不是必须的，如果没有设置路由键，则使用原队列的路由键</span></span><br><span class="line">map.put(<span class="string">"x-dead-letter-routing-key"</span>,<span class="string">"dlx-routing-key"</span>);</span><br><span class="line">MqQueue queue = <span class="keyword">new</span> MqQueue().name(queueName).arguments(map);</span><br><span class="line">amQueueDeclare.declareQueue(queue);</span><br></pre></td></tr></table></figure>
<p>这样就初步完成了死信队列的声明。其业务流程图如下：</p>
<p><img src="https://s2.ax1x.com/2019/07/04/ZUG0dU.png" alt></p>
<h4 id="延时队列（定时队列）"><a href="#延时队列（定时队列）" class="headerlink" title="延时队列（定时队列）"></a>延时队列（定时队列）</h4><p>上述的购物车订单的示例，其实最优的方案设计是使用TTL+DLX来实现，如果用户没有在规定的时间来支付，则这个订单就进行一场处理。</p>
<p>延时队列的具体使用方法如下：</p>
<p>方案一：</p>
<ol>
<li>声明一个设置死信队列的队列，该队列没有消费者</li>
<li>给每一个发送该队列的消息设置过期时间</li>
<li>消息一旦过期就会被死信队列消费，这样就能实现延时队列的效果</li>
</ol>
<p>方案二：</p>
<ol>
<li>声明多个设置死信队列、不同过期时间的队列，该队列没有消费者</li>
<li>然后通过不同的的routingKey来将消息发送到不同的队列上</li>
<li>等队列过期时间一到，消息就会匹配到死信队列上，这样也能实现延时队列的效果</li>
</ol>
<p>方案一和方案二的实现原理基本相同，不同的是一个是消息的过期时间，一个是队列的过期时间，方案二声明多个不同过期时间的队列，而方案一只声明一个队列，这样的话有优点也有缺点，优点是减少了RabbitMQ的队列数量，缺点是降低了RabbitMQ队列消息消费的速度，而使用哪种方案可以根据业务和流量来衡量使用</p>
<p>具体的业务流程图如下：</p>
<p><img src="https://s2.ax1x.com/2019/07/04/ZUaCqI.png" alt></p>
<h3 id="消息持久化"><a href="#消息持久化" class="headerlink" title="消息持久化"></a>消息持久化</h3><p>RabbitMQ中交换机、队列和消息都可以持久化，其中交换机和队列的持久化只需要在声明时，其属性durable为true即可，而消息的持久化是建立在队列持久化的基础上，因为在RabbitMQ中，消息时存储在队列上的，队列都没有了，消息肯定也是存储不了的。</p>
<p>消息的持久化在上面的内容已经介绍过了，不同版本的SpringBoot的RabbitMQ集成，实现的方式可能 不一样，但是本质都是一样的，最终操作的都是RabbitMQ服务器</p>
<p>消息和队列的持久化都是比较简单的，但是我们这里要清楚的知道，如果我们将所有的消息都设置了持久化了，会严重影响RabbitMQ的性能，毕竟消息写入到磁盘的速度比写入内存的数据慢的不是一点点的。</p>
<p>这个就要求我们在设计时需要注意，对于可靠性不是那么高的消息，可以不采用持久化处理来提高吞吐量。<strong>在选择是否将消息持久化时，需要在可靠性和吞吐量之间做一个权衡</strong></p>
<h2 id="消息消费处理"><a href="#消息消费处理" class="headerlink" title="消息消费处理"></a>消息消费处理</h2><h3 id="参数isAck"><a href="#参数isAck" class="headerlink" title="参数isAck"></a>参数isAck</h3><p>这里需要注意一点，在我们上一篇博文<a href="http://amos_zhu.gitee.io/blog/passages/SpringBoot整合RabbitMQ——消息的发送和接收/">SpringBoot整合RabbitMQ——消息的发送和接收</a>中，我们有介绍在为队列设置监听时，有个参数<code>isAck</code>，这里如果设置成true，则队列在接收到消息后，不管业务方有没有完全消费消息，都会给RabbitMQ返回个消息已经消费成功的结果，RabbitMQ在判断消息已经成功消费了则会删除队列中的消息，但是业务方其实没有真正完成消息的消费，这样就会导致数据的丢失。</p>
<p>那我们怎么来处理这个问题呢？</p>
<p>其实很简单，我们只需要将<code>isAck</code>字段的参数设置成false即可，这样的话就需要业务方手动去操作该消息有没有被成功消费。如果没有消费的话就拒绝这个消息，是的拒绝这个消息，还记得我们之前介绍过的死信队列，队列设置了死信队列，消息一旦被拒绝的话，消息就会进入死信交换机，进而匹配到响应的队列中，可以在队列中将消息持久化到本地，这是一种解决方法。</p>
<p>具体的流程图可以参考死信队列的流程图:<br><img src="https://s2.ax1x.com/2019/07/04/ZUG0dU.png" alt></p>
<h3 id="消息分发"><a href="#消息分发" class="headerlink" title="消息分发"></a>消息分发</h3><p>在实际的生产过程中，可能一个队列存在多个消费者，那么队列此时收到的消息就会以轮询的方式发送给消费者。</p>
<p>一般情况下，rabbitMQ会将第m条消息发送给第m%n个消费者。这里其实有个隐患，在消费者任务非常繁重的情况下，来不及消费那么多的消息，而其他的消费者，由于某些原因，很快的处理完消息，这种情况就很容易出现某个消费者承受的压力就比较大，造成整体应用的吞吐量下降。</p>
<p>为了解决这个问题， 我们其实可以设置Channel信道上的最大处理消息的个数，代码设置如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在系统初始化启动时，加载Connection，创建Channel，然后设置BasicQos的个数</span></span><br><span class="line">Channel channel = cachingConnectionFactory.createConnection().createChannel();</span><br><span class="line">channel.basicQos(<span class="number">1000</span>);</span><br></pre></td></tr></table></figure></p>
<p><code>basicQos</code>具体的作用是设置允许限制Channel上所有消费者所能保持的最大未确认的消息的数量</p>
<p><strong>注意如果这里设置了最大的未确认的消息的数量，那么所有的消费者都会生效</strong></p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#rabbitMQ如何保证消息的不丢失"><span class="toc-text">rabbitMQ如何保证消息的不丢失</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#消息设置mandatory参数和使用备份交换机"><span class="toc-text">消息设置mandatory参数和使用备份交换机</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#mandatory参数"><span class="toc-text">mandatory参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#备份交换机"><span class="toc-text">备份交换机</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#过期时间和死信队列"><span class="toc-text">过期时间和死信队列</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#过期时间"><span class="toc-text">过期时间</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#死信队列"><span class="toc-text">死信队列</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#延时队列（定时队列）"><span class="toc-text">延时队列（定时队列）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#消息持久化"><span class="toc-text">消息持久化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#消息消费处理"><span class="toc-text">消息消费处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#参数isAck"><span class="toc-text">参数isAck</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#消息分发"><span class="toc-text">消息分发</span></a></li></ol></li></ol>
  </div>
</aside>
  
    <aside class="passage-copyright">
      <div>本文作者: Amos Zhu</div>
      
        <div>
          原文链接: 
          <a href target="_blank">http://amos_zhu.gitee.io/passages/SpringBoot整合RabbitMQ——RabbitMQ进阶/</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/blog/tags/Spring-Boot/"><i class="fa fa-tags"></i>Spring Boot</a>
     
      <a href="/blog/tags/rabbitMQ/"><i class="fa fa-tags"></i>rabbitMQ</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    <div class="site-footer-wrapper">
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">博客推荐</h5>
          
            <span class="site-footer-item">
              <a href="http://youzhixueyuan.com/series/java" target="_blank">优知学院</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://ruanyifeng.com/" target="_blank">阮一峰的个人网站</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">系列教程</h5>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/redis" target="_blank">Redis系列教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/rabbitMQ/" target="_blank">RabbitMQ教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/kafka/" target="_blank">Kafka架构探险</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">抓到我</h5>
          
            <span class="site-footer-item">
              <a href="https://gitee.com/amos_zhu" target="_blank">Gitee</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://blog.csdn.net/wdcl2468" target="_blank">CSDN</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://www.jianshu.com/u/2dbe61b1f3c3" target="_blank">简书</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: amoszhu@aliyun.com
      </div>
    
    <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div>
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <div class="site-layer-input-choose">
          <a href="javascript:void(0);" title="Change Search Engine">Google</a>
        </div>
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">
  <div class="bottom-bar-left">
    <a href="/blog/passages/全局代码异常处理封装——让代码逼格更高点/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/passages/SpringBoot整合RabbitMQ——消息的发送和接收/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
      <a href="javascript:void(0);" id="site-reward">
        <i class="fa fa-thumbs-up"></i>
      </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
    <a id="share-btn-wechat" href="javascript:void(0);" target="_blank">
      <i class="fa fa-wechat"></i>
    </a>
  
</div>
    


  <script async>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>



  <script async>
    (function(){
    var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?":"https://jspassport.ssl.qhimg.com/11.0.1.js?";
    document.write('<script src="' + src + '" id="sozz"><\/script>');
    })();
  </script>


    
  </body>
</html>